package com.hui.controller.user;

import com.hui.entity.dao.ShopUser;
import com.hui.entity.dto.LocalUser;
import com.hui.entity.enums.AppHttpCodeEnum;
import com.hui.service.SystemConfigService;
import com.hui.service.UserService;
import com.hui.util.ResponseResult;
import com.hui.util.module.GetUserInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static com.hui.config.SystemConfigConstants.YSHOP_SHOW_RECHARGE;

/**
 * 用户控制器
 */
@RestController
@CrossOrigin
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/api")
public class UserController {
    private final UserService userService; //用户表
    private final SystemConfigService systemConfigService;

    /**
     * 用户资金统计
     */
    //(value = "查看用户资金统计", type = 1)
    @GetMapping("/user/balance")
    //(value = "用户资金统计",notes = "用户资金统计")
    public ResponseResult userBalance(HttpServletRequest request) {
        Long uid = GetUserInfo.getUid(request);
        ShopUser shopUser = userService.getById(uid);

        Map<String, Object> map = new HashMap<>();
        Double[] userMoneys = userService.getUserMoney(shopUser.getUid());
        //map.put("now_money",yxUser.getNowMoney());
        map.put("nowMoney", shopUser.getNowMoney());
        map.put("orderStatusSum", userMoneys[0]);//获取购买的商品 的消费总额
        map.put("recharge", userMoneys[1]);//从账单表获取累计充值金额
        map.put("is_hide", systemConfigService.getData(YSHOP_SHOW_RECHARGE));//配置表(SystemConfig) 获取一些配置信息
        return ResponseResult.okResult(map);
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/userInfo")
    public ResponseResult userInfo(HttpServletRequest request) {
        Long uid = GetUserInfo.getUid(request);
        ShopUser user = userService.getById(uid);
        /*查询用户是否存在end*/
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_ERROR);
        }
        LocalUser.setUser(user, user.getUid());//TODO 全局用户信息
        /*返回信息*/
//        EshopUserBo eshopUserBo = BeanCopyUtils.copyBean(user, EshopUserBo.class); //拷贝信息
//        String jsonEshopUserBo = JSON.toJSONString(eshopUserBo);
//        EshopUserVo eshopUserVo = BeanCopyUtils.copyBean(user, EshopUserVo.class); //拷贝用户信息
//        Map<String, Object> map = new HashMap<>();
//        map.put("user", eshopUserVo);

        return ResponseResult.okResult(userService.getNewYxUserById(user));
    }
}
